/*
 * IJA2011, Board game "Clovece nezlob se"
 * grp108
 * Tomáš Mičiak,xmicia00
 * Andrej Martinák, xmarti51
 *
 * Created on 5.5.2011, 0:56:42
 */

package src.clovece;

import java.awt.Point;
import java.net.*;
import java.io.*;
import src.clovece.game.GameInfo;
import src.clovece.game.Multiplayer;


/**
 * Dialog for registering onto the game server
 */
public class RegisterDialog extends javax.swing.JDialog {
    private GameFrame parent = null;
    private GameInfo gameInfo = GameInfo.getGameInfoInstance();

    /** Creates new form RegisterDialog */
    public RegisterDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        this.parent = (GameFrame) parent;
        Point p = parent.getLocation();
        this.setLocation(p.x +300, p.y +150);
    }

    public void showYourself() {
        initComponents();
        this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        NicknameLabel = new javax.swing.JLabel();
        PasswordLabel = new javax.swing.JLabel();
        ConfirmPasswordLabel = new javax.swing.JLabel();
        NicknameTextField = new javax.swing.JTextField();
        RegisterButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();
        PasswordField = new javax.swing.JPasswordField();
        ConfirmPasswordField = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel1.setText("New Player Registration");

        NicknameLabel.setText("Nickname:");

        PasswordLabel.setText("Password");

        ConfirmPasswordLabel.setText("Confirm password:");

        NicknameTextField.setColumns(11);

        RegisterButton.setText("Register");
        RegisterButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                RegisterButtonMouseClicked(evt);
            }
        });

        CancelButton.setText("Cancel");
        CancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                CancelButtonMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(114, 114, 114)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(PasswordLabel)
                            .addComponent(NicknameLabel)
                            .addComponent(ConfirmPasswordLabel)
                            .addComponent(RegisterButton, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(53, 53, 53)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(PasswordField)
                                    .addComponent(NicknameTextField)
                                    .addComponent(ConfirmPasswordField)))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(CancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(11, 11, 11)))))
                .addContainerGap(39, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(42, 42, 42)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(NicknameLabel)
                    .addComponent(NicknameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(PasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(ConfirmPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(ConfirmPasswordLabel)))
                    .addComponent(PasswordLabel))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(RegisterButton)
                    .addComponent(CancelButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CancelButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_CancelButtonMouseClicked
        this.dispose();
    }//GEN-LAST:event_CancelButtonMouseClicked

    private void RegisterButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_RegisterButtonMouseClicked
        String pass = new String();
        if(this.PasswordField.getPassword().length != this.ConfirmPasswordField.getPassword().length) {
            ErrorDialog rwd = new ErrorDialog(this, true, "Confirmation password doesn't match.");
            rwd.setVisible(true);
            return;
        }
        for(int i = 0; i < this.PasswordField.getPassword().length; i++) {
            if(this.PasswordField.getPassword()[i] != this.ConfirmPasswordField.getPassword()[i]) {
                ErrorDialog rwd = new ErrorDialog(this, true, "Confirmation password doesn't match.");
                rwd.setVisible(true);
                return;
            }
            else {
                pass += this.PasswordField.getPassword()[i];
            }
        }
        if(this.PasswordField.getPassword().length < 4) {
            ErrorDialog rwd = new ErrorDialog(this, true, "Entered password is too short.");
            rwd.setVisible(true);
            return;
        }
        String pwd = pass;
        String username = NicknameTextField.getText();
        this.dispose();
        this.registration(username,pwd);
    }//GEN-LAST:event_RegisterButtonMouseClicked


    public void registration(String username, String pwd)
    {
    
        String host = gameInfo.getHost();
        int port = 19999;

        StringBuffer instr = new StringBuffer();

         try {
         Multiplayer mp = new Multiplayer();
          mp.init();


          BufferedOutputStream bos = new BufferedOutputStream(this.gameInfo.getSocket().
              getOutputStream());

          OutputStreamWriter osw = new OutputStreamWriter(bos, "US-ASCII");

          String process = "Register:"+ username + "|" + pwd +  (char) 13;
          osw.write(process);
          osw.flush();
          this.gameInfo.getSocket().close();
         }
        catch (IOException f) {
            ErrorDialog ed = new ErrorDialog(this, true, "Cannot connect to the server");
            ed.setVisible(true);
            //System.out.println("IOException: " + f);
        }
        catch (Exception g) {
          System.out.println("Exception: " + g);
        }


  }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelButton;
    private javax.swing.JPasswordField ConfirmPasswordField;
    private javax.swing.JLabel ConfirmPasswordLabel;
    private javax.swing.JLabel NicknameLabel;
    private javax.swing.JTextField NicknameTextField;
    private javax.swing.JPasswordField PasswordField;
    private javax.swing.JLabel PasswordLabel;
    private javax.swing.JButton RegisterButton;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables

}
